//
//  GRError.m
//  YCGRICE
//
//  Created by Liu Feng on 2018/7/5.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRError.h"
#import "UpgradeAppBounces.h"
#import "GRBlankSpace.h"

static NSString *const GRErrorDomain = @"com.xiaoguikuaipao.GRice";

static NSString * const erMsg  = @"errorMessage";
static NSString * const ptMsg  = @"promptMessage";
static NSString * const ercode = @"errorCode";


@interface GRError () <GRBlankSpaceDelegate>

{
    BOOL displayERROR;
}

@property (nonatomic, copy) NSString *msg;
@property (nonatomic, strong) GRBlankSpace *blankSpace;

@end

@implementation GRError

- (instancetype)initWithErrorCode:(NSInteger)code userInfo:(nullable NSDictionary *)userInfo {
    GRError *error = [GRError errorWithDomain:GRErrorDomain code:code userInfo:userInfo];
    error.msg         = [error serverReturnError];
    return error;
}

-(NSString *)errorMsg {
    return [self errorAlertWithUIView:nil display:NO offsetY:0];
}

-(NSInteger)code {
    return [self.userInfo[ercode] integerValue];
}

-(NSString *)errorCode {
    return self.userInfo[ercode];
}

- (NSString *)errorAlertWithUIView:(UIView *)view display:(BOOL)display offsetY:(CGFloat)y {
    if ([self.domain isEqualToString:GRErrorDomain]) {

        return self.msg;
        
    }else {
        
        BlankContentMode mode = 0;
        if (self.code == NSURLErrorNotConnectedToInternet || self.code == NSURLErrorCannotFindHost) {
            self.msg  = @"哎呦，你是不是忘记打开网络了~";
            mode      = GRBlankContentModeBrokenNetwork;
        }else if (self.code == NSURLErrorTimedOut) {
            self.msg  = @"咦~你的网路好好好慢呀~";
            mode      = GRBlankContentModeRequestTimeOut;
        }else if (self.code == 3840) {
            self.msg  = @"OH~NO~，网络出错啦！";
            mode      = GRBlankContentModeServerDisconnection;
        }else if (self.code == NSURLErrorCannotConnectToHost || self.code == NSURLErrorBadServerResponse) {
            self.msg  = @"OH~NO~，服务器出错啦！";
            mode      = GRBlankContentModeNotFound;
        }else {
            self.normalNetworkConnection = YES;
            self.msg  = self.localizedDescription;
        }
        
        if (display && mode != 0) {
            [self errorMsgDisplay:view contentMode:mode offsetY:y];
        }
        
        return self.msg;
    }
}


/**
 服务器返回的错误信息

 @return <#return value description#>
 */
- (NSString *)serverReturnError {
    NSString *code = self.userInfo[ercode];
    if ([code isEqualToString:@"0xff0000"]) {
        [UpgradeAppBounces upgrade];
        return nil;
    }else if ([code integerValue] == 401) {
        [[GRUserBasisInfo shareBasis] LoginOut];
        [UIViewController displacementLogin];
        return nil;
    }else {
        return self.userInfo[ptMsg];
    }
}

- (void)errorAlertWithUIView:(UIView *)view offsetY:(CGFloat)y completion:(ERRORAlertBlock)completion {
    NSString *msg = [self errorAlertWithUIView:view display:YES offsetY:y];
    if (!displayERROR) {
        [GRHUD showErrorMsg:msg];
    }else {
        self.alertBlock = completion;
    }
}


#pragma Mark =================  断网等情况展示  ===================
- (void)errorMsgDisplay:(UIView *)view contentMode:(BlankContentMode)contentMode offsetY:(CGFloat)y {
    displayERROR = YES;
    
    CFRetain((__bridge CFTypeRef)(self));

    self.blankSpace.frame       = CGRectMake(0, y, view.width, view.height - y);
    self.blankSpace.contentMode = contentMode;
    [view addSubview:self.blankSpace];
}

-(void)didClickBlankButton:(UIButton *)sender {
    CFRelease((__bridge CFTypeRef)(self));

    [self.blankSpace removeFromSuperview];
    
    if (self.alertBlock) {
        self.alertBlock();
    }
}

-(GRBlankSpace *)blankSpace {
    if (!_blankSpace) {
        _blankSpace = [GRBlankSpace loadFromNib];
        _blankSpace.delegate = self;
    }
    return _blankSpace;
}

@end
